<?php

namespace backend\controllers;
use backend\models\News;
use backend\models\NewsFormat;
use backend\models\NewsTemp;
use \yii\web\Controller;
use Yii;

class NewsController extends Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }


    /*
     * 把采集的文章中的图片放入redis，待go下载图片
     */
    public function actionPutImgToRedis(){
        $imgs = NewsFormat::find()->select(['id','images','article_id'])->asArray()->all();


        foreach ($imgs as $img){
            //Yii::$app->redis->HSet('articleIdRelation',$img['article_id'],$img['id']);

//            $_imgs = json_decode($img['images'],true);
//            if(!empty($_imgs)){
//                foreach ($_imgs as $_img){
//                    Yii::$app->redis->LPush('ariticleImgs',$_img['img']);
//                }
//            }

        }

    }

    /*
     * 文章数据处理
     */
    public function actionPutRedis(){
        $data =  NewsTemp::find()->where("in_redis = 0")->orderBy('article_id,page ASC')->asArray()->all();
        $rs = [];
        $articleId = 0;
        foreach ($data as $vo){
            if($vo['article_id'] == $articleId){
                $temp = $rs[$vo['article_id']];
                $temp['content']  = $temp['content'].'|||'.$vo['content'];
                $temp['images'] = json_decode($temp['images'],true);
                $_images = json_decode($vo['images'],true);
                $temp['images'] = json_encode(array_merge($temp['images'],$_images));
                unset($_images);
                $rs[$vo['article_id']] = $temp;
            }else{
                $rs[$vo['article_id']] = $vo;
                $articleId = $vo['article_id'];
            }
        }
        foreach ($rs as $vo){
            $model = new NewsFormat();
            $model->setAttributes($vo);
            $model->save();
        }
    }

}


